package com.ali.service.impl;

import com.ali.dao.TransactionalSecondMapper;
import com.ali.entity.TransactionalSecond;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class TransactionalSecondServiceImpl {
    @Resource
    private TransactionalSecondMapper transactionalSecondMapper;

    /**
     * Propagation.REQUIRED：如果当前存在事务，则加入事务，否则新建事务。默认值
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void required(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content);
        transactionalSecondMapper.insert(transactionalSecond);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void requiredException(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content + "Exception");
        transactionalSecondMapper.insert(transactionalSecond);
        throw new RuntimeException("手动抛出异常");
    }

    /**
     * Propagation.SUPPORTS：如果当前存在事务，则加入事务，否则以非事务方式执行操作
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public void supports(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content);
        transactionalSecondMapper.insert(transactionalSecond);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void supportsException(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content + "Exception");
        transactionalSecondMapper.insert(transactionalSecond);
        throw new RuntimeException("手动抛出异常");
    }

    /**
     * Propagation.MANDATORY：如果当前存在事务，则加入事务，否则抛出异常
     */
    @Transactional(propagation = Propagation.MANDATORY)
    public void mandatory(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content);
        transactionalSecondMapper.insert(transactionalSecond);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void mandatoryException(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content + "Exception");
        transactionalSecondMapper.insert(transactionalSecond);
        throw new RuntimeException("手动抛出异常");
    }

    /**
     * Propagation.REQUIRES_NEW：如果当前存在事务，则挂起当前事务。创建一个新事务
     * 子事务可单独提交，可单独回滚
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void requiresNew(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content);
        transactionalSecondMapper.insert(transactionalSecond);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void requiresNewException(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content + "Exception");
        transactionalSecondMapper.insert(transactionalSecond);
        throw new RuntimeException("手动抛出异常");
    }

    /**
     * Propagation.NOT_SUPPORTED：如果当前存在事务，则挂起当前事务。以非事务方式执行操作
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void notSupported(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content);
        transactionalSecondMapper.insert(transactionalSecond);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void notSupportedException(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content + "Exception");
        transactionalSecondMapper.insert(transactionalSecond);
        throw new RuntimeException("手动抛出异常");
    }

    /**
     * Propagation.NEVER：如果当前存在事务，则抛出异常，否则以非事务方式执行操作
     */
    @Transactional(propagation = Propagation.NEVER)
    public void never(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content);
        transactionalSecondMapper.insert(transactionalSecond);
    }

    @Transactional(propagation = Propagation.NEVER)
    public void neverException(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content + "Exception");
        transactionalSecondMapper.insert(transactionalSecond);
        throw new RuntimeException("手动抛出异常");
    }

    /**
     * Propagation.NESTED：如果当前存在事务，则在嵌套事务内执行，否则新建事务
     * 子事务不可单独提交，可单独回滚
     */
    @Transactional(propagation = Propagation.NESTED)
    public void nested(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content);
        transactionalSecondMapper.insert(transactionalSecond);
    }

    @Transactional(propagation = Propagation.NESTED)
    public void nestedException(String content) {
        TransactionalSecond transactionalSecond = new TransactionalSecond();
        transactionalSecond.setContent(content + "Exception");
        transactionalSecondMapper.insert(transactionalSecond);
        throw new RuntimeException("手动抛出异常");
    }
}
